package com.tuo51.evaluate.controller;


import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.model.StatusResponse;
import com.matt.commons.page.PagerQuery;
import com.matt.foundation.constant.RequestConstant;
import com.matt.foundation.dto.AreaDetailDTO;
import com.matt.foundation.model.MemVerificationCache;
import com.matt.service.remoteCall.SsoCallService;
import com.tuo51.evaluate.repositories.MailRecordRepository;
import com.tuo51.evaluate.service.EvaluateV2ExportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import static com.matt.foundation.constant.RequestConstant.TICKET;

//import com.tuo51.core.service.StaffService;


@RestController
@RequestMapping("/eapi/exports")
@Api(value = "/eapi/exports", tags = "测评 - 导出")
public class ExportRecordController {

    @Autowired
    private MailRecordRepository mailRecordRepository;

    @Autowired
    private EvaluateV2ExportService exportService;

    @Autowired
    private MemVerificationCache cache;

    @Autowired
    private SsoCallService ssoCallService;

    @ApiOperation(value = "邮件记录",httpMethod = "GET")
    @GetMapping(value = "/mail_records")
    public StatusResponse newsList(@RequestHeader(TICKET) String ticket,
                                   PagerQuery pagerQuery){
        if (pagerQuery.getPageSize() <= 0){
            pagerQuery.setPageSize(10);
        }

        String staffId = cache.getStaffId(ticket);

        PageRequest pageRequest = PageRequest.of(pagerQuery.getPageNum(), pagerQuery.getPageSize());

        return StatusResponse.success(mailRecordRepository.findAllByStaffIdOrderByCreatedDesc(staffId, pageRequest));
    }

    @ApiOperation(value = "导出 word 报告",httpMethod = "GET")
    @RequestMapping(value = "/word_export/{evaluateRecordId}", method = RequestMethod.GET)
    public StatusResponse exportWordReport(@RequestHeader(RequestConstant.TICKET) String ticket,
                                           @RequestHeader(value = RequestConstant.APP_CHANNEL, required = false) String channel,
                                           @PathVariable("evaluateRecordId") String evaluateRecordId)  {
        StaffDTO staff = cache.getStaff(ticket);
        AreaDetailDTO area = ssoCallService.getAreaDetailDTO(ticket);

        String exported_file = exportService.exportEvaluateWordReportToOSS(channel, evaluateRecordId, staff, area);

        return StatusResponse.success(exported_file);
    }

    @ApiOperation(value = "导出 word 对比报告",httpMethod = "GET")
    @RequestMapping(value = "/word_export_comparing_report", method = RequestMethod.GET)
    public StatusResponse exportWordComparingReport(@RequestHeader(RequestConstant.TICKET) String ticket,
                                                    @RequestParam("evaluateRecordId") String evaluateRecordId,
                                                    @RequestParam("comparingRecordId") String comparingRecordId)  {
        StaffDTO staff = cache.getStaff(ticket);
        AreaDetailDTO area = ssoCallService.getAreaDetailDTO(ticket);

        String exported_file = exportService.exportEvaluateWordReportToOSS(null, evaluateRecordId, staff, area);

        return StatusResponse.success(exported_file);
    }
}
